package com.lucy.property.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.lucy.common.constant.HttpStatus;
import com.lucy.common.domain.ResponseDTO;
import com.lucy.property.entity.BuildingUnit;
import com.lucy.property.entity.ParkingLot;
import com.lucy.property.module.park.checkpoint.CheckPoint;
import com.lucy.property.module.park.checkpoint.CheckPointService;
import com.lucy.property.module.park.checkpoint.params.CheckPointCreateParams;
import com.lucy.property.module.park.checkpoint.params.CheckPointQueryParams;
import com.lucy.property.module.park.checkpoint.params.CheckPointUpdateParams;
import com.lucy.property.service.ParkingLotService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.annotations.Check;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * @author kindear
 */
@RestController
@RequestMapping("park")
@Api(tags = "道闸管理")
public class CheckPointController {

    @Autowired
    CheckPointService service;

    @Autowired
    ParkingLotService lotService;

    @PostMapping("checkpoints")
    @ApiOperation(value = "道闸新增",notes = "")
    public ResponseDTO<Object> checkPointCreate(@RequestBody @Validated CheckPointCreateParams params){
        // 获取停车场信息
        Optional<ParkingLot> lotOp = lotService.getOne(params.getParkingLotId());
        if (!lotOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND);
        }
        ParkingLot lot = lotOp.get();
        CheckPoint point = new CheckPoint();
        point.setParkingLotName(lot.getParkingLotName());
        BeanUtil.copyProperties(params,point);
        return ResponseDTO.succData(
                service.saveOne(point)
        );
    }

    @DeleteMapping("checkpoints/{id}")
    @ApiOperation(value = "道闸删除",notes = "")
    public ResponseDTO<Object> checkPointDelete(@PathVariable("id") String id){
        service.deleteOne(id);
        return ResponseDTO.succ();
    }

    @PutMapping("checkpoints")
    @ApiOperation(value = "道闸更新",notes = "")
    public ResponseDTO<Object> checkPointUpdate(@RequestBody @Validated CheckPointUpdateParams params){
        Optional<CheckPoint> pointOp = service.getOne(params.getId());
        if (!pointOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND);
        }
        CheckPoint point = pointOp.get();
        BeanUtil.copyProperties(params,point);
        return ResponseDTO.succData(
                service.updateOne(point)
        );
    }

    @GetMapping("checkpoints")
    @ApiOperation(value = "道闸列表",notes = "")
    public ResponseDTO<Object> checkPointListQuery(CheckPointQueryParams params){
        Sort sort;
        if("DESC".equalsIgnoreCase(params.getOrder())){
            sort = Sort.by(Sort.Direction.DESC, params.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, params.getSortBy());
        }

        // tips : 页码对应 - 1
        Pageable pageable = PageRequest.of(params.getPageNum() - 1 , params.getPageSize(), sort);
        /*
        构建 Example
         */
        CheckPoint item = new CheckPoint();
        BeanUtil.copyProperties(params,item,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<CheckPoint> example = Example.of(item);
        return ResponseDTO.succData(service.getAll(pageable,example));
    }

    @GetMapping("checkpoints/{id}")
    @ApiOperation(value = "道闸查询",notes = "")
    public ResponseDTO<Object> checkPointQuery(@PathVariable("id") String id){
        Optional<CheckPoint> itemOp = service.getOne(id);
        return itemOp.<ResponseDTO<Object>>map(ResponseDTO::succData).orElseGet(() -> ResponseDTO.wrap(HttpStatus.NOT_FOUND));
    }
}
